package com.czy.daishusys.adapter;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.czy.daishusys.R;
import com.czy.daishusys.entity.HomeOrderModel;
import com.czy.daishusys.ui.order.DeliverGoodsActivity;
import com.czy.daishusys.ui.order.OrderDetailActivity;
import com.czy.daishusys.widget.MyRecyclerView;

import java.util.List;

/**
 * Created by asus on 2017/11/29.
 * 首页订单
 */


public class HomeOrderAdapter extends BaseMultiItemQuickAdapter<HomeOrderModel.DataBean.ListBean, BaseViewHolder> {


    public HomeOrderAdapter(List<HomeOrderModel.DataBean.ListBean> data) {
        super(data);
        addItemType(HomeOrderModel.ORDER_NORMAL_TYPE, R.layout.item_home_order_recycler);
        addItemType(HomeOrderModel.ORDER_EMPTY_TYPE, R.layout.item_home_order_empty);
    }


    @Override
    protected void convert(BaseViewHolder holder, final HomeOrderModel.DataBean.ListBean item) {
        switch (item.getItemType()) {
            case HomeOrderModel.ORDER_NORMAL_TYPE:
                List<HomeOrderModel.DataBean.ListBean.OrderItemsBean> orderItems = item.getOrderItems();
                final HomeOrderItemAdapter adapter = new HomeOrderItemAdapter(R.layout.item_home_order_item_recycler, orderItems);
                MyRecyclerView rvOrderGoods = (MyRecyclerView) holder.getView(R.id.rv_order_goods);
                rvOrderGoods.setAdapter(adapter);
                rvOrderGoods.setLayoutManager(new LinearLayoutManager(mContext));
                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent = new Intent(mContext, OrderDetailActivity.class);
                        intent.putExtra("orderId", item.getUserOrderId());
                        mContext.startActivity(intent);
                    }
                });
                holder.getView(R.id.tv_order_deliver).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, DeliverGoodsActivity.class);
                        intent.putExtra("orderId", item.getUserOrderId());
                        mContext.startActivity(intent);
                    }
                });
                int count = 0;
                double denomination = 0;
                for (int i = 0; i < orderItems.size(); i++) {
                    count += orderItems.get(i).getCount();
                    denomination += orderItems.get(i).getDiscountPrice();
                }
                holder.setText(R.id.tv_order_number, "订单编号:" + item.getOrderNumber())
                        .setText(R.id.tv_order_goods_statistics, "共" + count + "件商品  合计: ")
                        .setText(R.id.tv_order_goods_price, "¥" + (String.format("%.2f", (item.getTotal() - denomination) >= 0 ? (item.getTotal() - denomination) : 0)))
                        .setText(R.id.tv_order_goods_tips, "(含运费: ¥" + String.format("%.2f", item.getFreight()) + ")")
                        .setText(R.id.tv_order_sign, ("system".equals(item.getOrderSubType()) ||"system_tticar".equals(item.getOrderSubType()))? "平台商品" : "转移商品");

                break;
            case HomeOrderModel.ORDER_EMPTY_TYPE:


                break;
        }
    }


}
